import { wxLogin } from './userapi';

// request.js
function request(url, method, data, header = {}) {
	const loginInfo = uni.getStorageSync("loginInfo");
	if (loginInfo) {
		header.accessToken = loginInfo.accessToken; 
	}
    return new Promise((resolve, reject) => {
        uni.request({
            url: url,
            method: method,
            data: data,
            header: header,
            success: (res) => {
                console.log('http', res, res.data.code);
                if (res.statusCode === 200) {
                    resolve(res.data);
                } else if (res.statusCode == 401) {
                    uni.showToast({
                        title: '网络错误，请重试',
                        icon: 'none'
                    });
                    reject(0);
                } else if (res.statusCode == 400) {
                    uni.showToast({
                        title: '服务端出错，请联系管理员或稍后再试',
                        icon: 'none'
                    });
                    reject(0);
                } else {
                    uni.showToast({
                        title: '服务端出错，请联系管理员或稍后再试',
                        icon: 'none'
                    });
                    reject(0);
                }
            },
            fail: (err) => {
                console.log('http', err);
                reject(err);
            }
        });
    });
}
export function get(url, data = {}) {
    return request(url, 'GET', data, {
        Authorization: uni.getStorageSync('loginInfo').accessToken
    });
}
export function postByJson(url, data = {}) {
    return request(url, 'POST', data, {
        Authorization:uni.getStorageSync('loginInfo').accessToken,
        'content-type': 'application/json' // 根据需求设置请求头
    });
}

export function post(url, data) {
    return request(url, 'POST', data, {
        Authorization:uni.getStorageSync('loginInfo').accessToken
    });
}
